package com.wdzl.util;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.text.html.ImageView;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * 注意：图片需要放在src目录下并编译完成后才可以
 */
public class ImageUtil {

    public  static Image get(String fileName){
        System.out.println(">>>>img:"+fileName);
        InputStream resourceAsStream = ImageUtil.class.getResourceAsStream( fileName);
        try {
            return  ImageIO.read(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Image getByImgs(String  imgName){
        imgName = "/imgs/"+imgName+".png";
        return get(imgName);
    }

    /**
     * 通过文件名获取ImageIcon
     * @param filename
     * @return
     */
    public static ImageIcon getImageIcon(String filename){
        return new ImageIcon(getSrcFileResources(filename));
    }

    public static ImageIcon getImageIconByImgs(String imgName){
        imgName = "/imgs/"+imgName+".png";
        return getImageIcon(imgName);
    }

    public static URL getSrcFileResources(String filename){
        if(!filename.startsWith("/")){
            filename = "/"+filename;
        }
        return FileUitl.class.getResource(filename);
    }
}
